<?php
namespace app\model;

use Lcobucci\JWT\Parser;
use Lcobucci\JWT\ValidationData;
use think\Model;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key;

class Config extends Model
{
    protected $name = 'config';

    /**
     * 获取config列表
     */
    public function  configList($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $list = self::where(['is_del'=>0])->order('id','desc')->select();
        return getSuccessResponse('查询成功',$list);
    }

    /**
     * 新增编辑config的key和value
     */
    public function  addEditConfig($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $key = $param['key'] ?? '';
        $value = $param['value'] ?? '';
        $remark = $param['remark'] ?? '';
        $id = $param['id'] ?? '';
        if (empty($key) || empty($value)){
            return getFailResponse('参数有误');
        }
        $configInfo = self::where(['key'=>$key])->find();
        if (empty($id)){
            if (!empty($configInfo)){
                return getFailResponse('key已存在');
            }
            $data =[
                'key'=>$key,
                'value'=>$value,
                'remark'=>$remark,
                'create_time'=>time(),
                'update_time'=>time(),
            ];
            self::create($data);
        }else{
            self::where(['id'=>$id])->update(['value'=>$value, 'remark'=>$remark,'update_time'=>time()]);
        }
        return getSuccessResponse();
    }

    /**
     * 删除config
     */
    public function  delConfig($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $ids= $param['ids'] ?? '';
        if (empty($ids)){
            return getFailResponse('参数有误');
        }
        self::where('id','in',$ids)->update(['is_del'=>1,'update_time'=>time()]);
        return getSuccessResponse('删除成功');
    }

    //获取token
    public function  getToken($mobile='',$user_id='',$admin_id='',$expireSecond=86400){
        $signer =  new Sha256();
        $time  = time();
        $token = (new  Builder())
            ->identifiedBy(getTokenIdentify(),true)
            ->issuedAt($time)
            ->expiresAt($time+$expireSecond)   //过期时间 现在是60秒
            ->with('mobile',$mobile)   //app登陆 用户手机号
            ->with('user_id',$user_id)  //app登陆用户id
            ->with('admin_id',$admin_id)  //后台登陆用户id
            ->sign($signer, getTokenKey())
            ->getToken();
        return  (string)$token;
    }

    //解析token
    public  function   verifyToken($token){
        $token = (new Parser())->parse($token);
        //验证token
        $data =  new ValidationData();
        $data->setId(getTokenIdentify());//验证token标识
        $result  =  $token->validate($data);
        if (!$result){
            $res=[
                'user_id'=>'',
                'mobile'=>'',
                'admin_id'=>'',
            ];
        }else{
            $res = [
                'user_id'=>$token->getClaim('user_id'),
                'mobile'=>$token->getClaim('mobile'),
                'admin_id'=>$token->getClaim('admin_id'),
            ];
        }
        return $res;
    }

}